package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.RoleBean;
import com.project.bean.RootBean;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper mapper;

    @Override
    public PageInfo<RoleBean> findAll(int pageNO) {
        PageHelper.startPage(pageNO,PAGE_SIZE);
        PageInfo<RoleBean>  pageInfo=PageInfo.of(mapper.findAll());
        return pageInfo;
    }

    @Override
    public String addRole(RoleBean role, int[] rootIDArray) {
        if(mapper.checkRole(role.getName()) != 0){
            return "no";
        }
        mapper.addRole(role);
        mapper.addRoleRoot(role.getId(),rootIDArray);

        return "ok";
    }

    @Override
    public void delRole(int id) {
        mapper.delRole(id);
    }

    @Override
    public RoleBean findById(int id) {
        return mapper.findById(id);
    }

    @Override
    public List<RootBean> findRoots(int[] id) {
        return mapper.findRoots(id);
    }

    @Override
    public void update(int id, int[] idArray) {
        mapper.update(id,idArray);
    }
}
